<%
  @flavor = typecast_params.nonempty_str("flavor", PostgresResource::Flavor::STANDARD)
  @flavor = PostgresResource::Flavor::STANDARD unless Option::POSTGRES_FLAVOR_OPTIONS.any? { |k,| k == @flavor }
  @prices = fetch_location_based_prices("PostgresVCpu", "PostgresStorage")
  @has_valid_payment_method = @project.has_valid_payment_method?
  effective_quota = @project.effective_quota_value("PostgresVCpu")
  current_usage = @project.current_resource_usage("PostgresVCpu")
  @enabled_postgres_sizes = Option::VmSizes.select { effective_quota >= current_usage + it.vcpus }.map(&:name)
  @option_tree, @option_parents = generate_postgres_options(flavor: @flavor)

  @page_title, logo = postgres_flavors[@flavor].yield_self { ["Create #{it.title}", "logo-#{it.brand}.png"] }
  logo = nil if @flavor == PostgresResource::Flavor::STANDARD
%>

<%== render("components/billing_warning") %>

<%== part(
  "components/page_header",
  breadcrumbs: [
    %w[Projects /project],
    [@project.name, @project.path],
    ["PostgreSQL Databases", "#{@project.path}/postgres"],
    %w[Create #]
  ],
  right_items: [logo ? "<img src=\"/#{logo}\" class=\"h-6 object-contain\"/>" : nil]
) %>

<%
  byoc_description_html = "<p class='mt-1 text-sm leading-6 text-gray-600'>To run Ubicloud Postgres in your own AWS account using <a href='https://www.ubicloud.com/postgresql-on-aws-high-iops' class='text-orange-600 hover:text-orange-700'>BYOC (AWS)</a>, email us at <a href='mailto:support@ubicloud.com' class='text-orange-600 hover:text-orange-700'>support@ubicloud.com</a></p>"
  additional_element_html = "<label class=\"form_flavor form_flavor_standard cursor-not-allowed\"><input type='radio' name='byoc' value='byoc' class='peer sr-only' disabled><span class=\"radio-small-card justify-center p-3 text-sm font-semibold pointer-events-none bg-gray-100 text-gray-500\">Your Own AWS Account</span></label>"

  form_elements = [
    {name: "flavor", type: "hidden", value: @flavor},
    {name: "name", type: "text", label: "Name", required: "required", placeholder: "Enter name", opening_tag: "<div class='sm:col-span-3'>"},
    {name: "location", type: "radio_small_cards", label: "Location", required: "required", additional_element_html:, description_html: byoc_description_html, content_generator: ContentGenerator::Postgres.method(:location)},
    {name: "family", type: "radio_small_cards", label: "Server family", required: "required", content_generator: ContentGenerator::Postgres.method(:family)},
    {name: "size", type: "radio_small_cards", label: "Server size", required: "required", content_generator: ContentGenerator::Postgres.method(:size)},
    {name: "storage_size", type: "radio_small_cards", label: "Storage size", required: "required", content_generator: ContentGenerator::Postgres.method(:storage_size)},
    {name: "version", type: "radio_small_cards", label: "Version", required: "required", content_generator: ContentGenerator::Postgres.method(:version)},
    {name: "ha_type", type: "radio_small_cards", label: "High Availability", required: "required", content_generator: ContentGenerator::Postgres.method(:ha_type)},
  ]

  if [PostgresResource::Flavor::PARADEDB, PostgresResource::Flavor::LANTERN].include?(@flavor)
    form_elements << {name: "partnership_notice", type: "partnership_notice", label: "Partnership Notice", required: "required", content_generator: ContentGenerator::Postgres.method(:partnership_notice)}
  end

  action = "#{@project.path}/postgres"
%>

<%== part("components/form/resource_creation_form", action:, form_elements:, option_tree: @option_tree, option_parents: @option_parents) %>
